package com.ygqh.baby.quartz;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.ygqh.baby.po.ScheduleJob;
import com.ygqh.baby.utils.SpringUtils;

public class TaskUtils {
	public final static Logger log = Logger.getLogger(TaskUtils.class);

	/**
	 * 通过反射调用scheduleJob中定义的方法 方法参数只支持基本类型与String
	 * 
	 * @param <T>
	 * 
	 * @param scheduleJob
	 * @throws Exception
	 */
	public static <T> void invokMethod(ScheduleJob scheduleJob) throws Exception {
		Object object = null;
		Class<?> clazz = null;
		if (StringUtils.isNotBlank(scheduleJob.getSpringId())) {
			object = SpringUtils.getBean(scheduleJob.getSpringId());
		} else if (StringUtils.isNotBlank(scheduleJob.getBeanClass())) {
			clazz = Class.forName(scheduleJob.getBeanClass());
			object = clazz.newInstance();

		}

		invoke(object, clazz, scheduleJob.getMethodName(), scheduleJob.getDataMap());

		log.info("任务名称 = [" + scheduleJob.getJobName() + "]----------启动成功");
	}

	public static void invoke(Object object, Class<?> clazz, String methodName, Map<String, Object> dataMap) throws Exception {
		clazz = object.getClass();
		Method method = null;
		Class<?>[] paramClazz = null;

		paramClazz = getParameterTypes(clazz, methodName);
		if (paramClazz != null) {
			method = clazz.getDeclaredMethod(methodName, paramClazz);
		} else {
			method = clazz.getDeclaredMethod(methodName);
		}

		if (method != null) {
			if (paramClazz != null) {
				method.invoke(object, dataMap.values().toArray(new Object[0]));
			} else {
				method.invoke(object);
			}
		}

	}

	// private static Object[] getParameterValues(Class<?>[] paramClazz, SchJob
	// scheduleJob) {
	// Map<String, Object> map = scheduleJob.getDataMap();
	// Object[] objs = new Object[paramClazz.length];
	// String jobName = scheduleJob.getJobName();
	// if (jobName.startsWith(YgQuartzJob.Balance.name())) {
	// objs[0] = castType(paramClazz[0].getName(), map.get("orderId"));
	//
	// }
	// return objs;
	// }

	private static Class<?>[] getParameterTypes(Class<?> clazz, String methodName) {
		Method[] methods = clazz.getMethods();
		for (int i = 0; i < methods.length; i++) {
			Method method = methods[i];
			if (methods[i].getName().equals(methodName)) {
				return method.getParameterTypes();
			}
		}
		return null;

	}

	private static Object castType(String type, String value) {
		Object obj = value;
		if ("java.lang.Integer".equals(type) || "int".equals(type)) {
			obj = Integer.valueOf(value);
		} else if ("java.lang.Float".equals(type) || "float".equals(type)) {
			obj = Float.valueOf(value);
		} else if ("java.lang.Boolean".equals(type) || "boolean".equals(type)) {
			obj = Boolean.valueOf(value);
		} else if ("java.lang.Long".equals(type) || "long".equals(type)) {
			obj = Long.valueOf(value);
		} else if ("java.lang.Short".equals(type) || "short".equals(type)) {
			obj = Short.valueOf(value);
		} else if ("java.lang.Double".equals(type) || "double".equals(type)) {
			obj = Double.valueOf(value);
		} else if ("java.lang.Byte".equals(type) || "byte".equals(type)) {
			obj = Byte.valueOf(value);
		} else if ("java.lang.Character".equals(type) || "char".equals(type)) {
			obj = Character.valueOf(value.charAt(0));
		}
		return obj;
	}
}
